/*
 * Please leave this Copyright notice in your code if you use it
 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 */
namespace TextExtractors
{
    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel.Composition;
    using CommonUtils;

    [Export(typeof(ITextImport))]
    //[ExportMetadata("SpecialFormat", false)]
    [ExportMetadata("Description", "Rtf files")]
    [ExportMetadata("FileExtension", "*.rtf")]
    [ExportMetadata("ContextMenuText", "&Translate")]
    public class RtfFileReader : BaseTextExtractor ,ITextImport
    {


        #region ITextImport Members
        public string GetAllTextFromFile(string fileName,out Encoding encoding)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(null, "fileName");
            }
            if (!File.Exists(fileName))
            {
                throw new ArgumentException("File '" + fileName + "' is not found");
            }
            RichTextBox box = new RichTextBox();
            try
            {
                var arg = new ProgressEventArgs();
                arg.Description = "Opening Rtf file";
                arg.Progress = 0;
                if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
                    ProgressChanged(this, arg);
                using (StreamReader reader = new StreamReader(fileName, true))//GetEncoding(0x4e3)))
                {
                    string str2 = reader.ReadToEnd();
                    encoding = reader.CurrentEncoding;
                    box.Rtf = str2;
                    return box.Text;
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Error during text extraction from Rtf-file: " + fileName + Environment.NewLine + exception.Message);
            }
            //return "";
        }

        //public string FileDescription
        //{
        //    get { return "Rtf files"; }
        //}

        //public string FileExtension
        //{
        //    get { return "*.rtf"; }
        //}
        public event EventHandler ProgressChanged;
        #endregion
    }
}

